﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Data.SqlClient;
using Jungle.Code.DAL;

namespace Jungle.Code.Core
{
	public class Section
	{
		#region Fields
		private int _sectionID;
		private int _nodeID;
		private int _moduleID;
		private string _container;
		private int _ord;
		private bool _enabled;
		#endregion

		#region Properties
		public int SectionID {
			get {
				return _sectionID;
			}
			set {
				_sectionID = value;
			}
		}
		public int NodeID {
			get {
				return _nodeID;
			}
			set {
				_nodeID = value;
			}
		}
		public int ModuleID {
			get {
				return _moduleID;
			}
			set {
				_moduleID = value;
			}
		}
		public string Container {
			get {
				return _container;
			}
			set {
				_container = value;
			}
		}
		public int Ord {
			get {
				return _ord;
			}
			set {
				_ord = value;
			}
		}
		public bool Enabled {
			get {
				return _enabled;
			}
			set {
				_enabled = value;
			}
		}
		#endregion

		public static List<Section> GetByNodeID(int nodeID) {
			List<Section> sects = new List<Section>();
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crSections_GetByNodeID", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("nodeID", SqlDbType.Int).Value = nodeID;
				conn.Open();
				SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (dr.HasRows) {
					while (dr.Read()) {
						Section sect = new Section();
						SetAllFields(sect, dr);
						sects.Add(sect);
					}
				}
				dr.Close();
			}
			return sects;
		}

		public static bool Insert(Section sect) {
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crSections_Insert", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("nodeID", SqlDbType.Int).Value = sect._nodeID;
				command.Parameters.Add("moduleID", SqlDbType.Int).Value = sect._moduleID;
				command.Parameters.Add("container", SqlDbType.VarChar, 50).Value = sect._container;
				command.Parameters.Add("ord", SqlDbType.Int).Value = sect._ord;
				command.Parameters.Add("enabled", SqlDbType.Bit).Value = sect._enabled;
				conn.Open();
				command.ExecuteNonQuery();
				conn.Close();
			}
			return true;
		}

		public static bool DeleteBySectionID(int sectionID) {
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crSections_DeleteBySectionID", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("sectionID", SqlDbType.Int).Value = sectionID;
				conn.Open();
				command.ExecuteNonQuery();
				conn.Close();
			}
			return true;
		}

		public static bool SetEnabledBySectionID(int sectionID, bool enabled) {
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crSections_SetEnabledBySectionID", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("sectionID", SqlDbType.Int).Value = sectionID;
				command.Parameters.Add("enabled", SqlDbType.Bit).Value = enabled;
				conn.Open();
				command.ExecuteNonQuery();
				conn.Close();
			}
			return true;
		}

		public static Section GetBySectionID(int sectionID) {
			SqlConnection conn = new SqlConnection(DBHelper.ConnectionString);
			using (SqlCommand command = new SqlCommand("dbo.crSections_GetBySectionID", conn)) {
				command.CommandType = CommandType.StoredProcedure;
				command.Parameters.Add("sectionID", SqlDbType.Int).Value = sectionID;
				conn.Open();
				SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
				Section sect = new Section();
				if (dr.HasRows) {
					dr.Read();
					SetAllFields(sect, dr);
				}
				dr.Close();
				return sect;
			}
		}


		private static void SetAllFields(Section sect, SqlDataReader dr) {
			sect._sectionID = (int)dr["sectionID"];
			sect._nodeID = (int)dr["nodeID"];
			sect._moduleID = (int)dr["moduleID"];
			sect._container = (string)dr["container"];
			sect._ord = (int)dr["ord"];
			sect._enabled = (bool)dr["enabled"];
		}

	}
}
